package pl.edu.agh.io.gpstracker.dao;

import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import pl.edu.agh.io.gpstracker.model.User;

/**
 * 
 * @author wkedzior
 * 
 */
@Repository
public class UserDaoImpl extends GenericDaoImpl<User, Long> implements UserDao {

	public UserDaoImpl() {
		super(User.class);
	}

	@Override
	public User login(String login, String password) throws DataAccessException {
		// brak czasu!! jak sie robi uniqa? straszny shiit!!!!!!!!!!!! mega
		@SuppressWarnings("unchecked")
		List<User> result = getHibernateTemplate()
				.find("from pl.edu.agh.io.gpstracker.model.User where login = ? and password = ? ",
						new Object[] { login, password });
		if (result == null || result.isEmpty()) {
			return null;
		} else {
			return result.get(0);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> findAllByType(Short type) throws DataAccessException {
		if (type == null) {
			return findAll();
		} else {
			return getHibernateTemplate().find(
					"from pl.edu.agh.io.gpstracker.model.User  where type = ?",
					type);
		}
	}

	@Override
	public void deleteById(Long id) {
		User user = (User) getHibernateTemplate().
				find(" from  pl.edu.agh.io.gpstracker.model.User  where id = ?",
						(int)id.longValue()).get(0);
		delete(user);
	}
}
